package nc.ac.massey.cs.plugin;

import java.io.File;
import java.io.FilenameFilter;
import java.util.Iterator;

import nz.ac.massey.cs.domain.IConstraint;
import nz.ac.massey.cs.domain.ISudoku;
import nz.ac.massey.cs.domain.Sudoku;
import org.eclipse.swt.SWT;
import org.eclipse.swt.browser.Browser;
import org.eclipse.swt.custom.CTabFolder;
import org.eclipse.swt.custom.CTabItem;
import org.eclipse.swt.custom.SashForm;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeItem;
import org.java.plugin.ObjectFactory;
import org.java.plugin.PluginManager;
import org.java.plugin.PluginManager.PluginLocation;
import org.java.plugin.registry.PluginDescriptor;
import org.java.plugin.standard.StandardPluginLocation;

public class ManagePlugins 
{
	 public static void main(String[] args) 
	 {
	        ManagePlugins myclass = new ManagePlugins();
	        String constraint = "unique";
	       
	        myclass.loadPlugins();
	        myclass.getconstraintplugins(constraint);
	       // myclass.addguicrap();
	   
	     
	    }
	
	 private static PluginManager pluginManager ;
	 private static void loadPlugins() 
	 {
		 pluginManager = ObjectFactory.newInstance().createManager(); 
		 File pluginsDir = new File("sudokuPlugins");
		 System.out.println(pluginsDir.getAbsolutePath());
		 
		 File[] plugins = pluginsDir.listFiles(new FilenameFilter() {

				public boolean accept(File dir, String name) {
					return name.toLowerCase().endsWith(".zip");
				}
	    		
	    	});
		 try {
				
				PluginLocation[] locations = new PluginLocation[plugins.length];
				
				for (int i = 0; i < plugins.length; i++) {
					locations[i] = StandardPluginLocation.create(plugins[i]);
				}

				pluginManager.publishPlugins(locations);
				 System.out.print(plugins.length);
			} catch (Exception e) {
				throw new RuntimeException(e);
			}
		
		 
	 }
	 
	public static IConstraint getconstraintplugins(String constraint)
	 {
		if(pluginManager == null)
		{
			loadPlugins();
		}
		 try
		 {
			 loadPlugins();
			 Iterator it = pluginManager.getRegistry().getPluginDescriptors().iterator();
			 System.out.print(it.toString());
			 while(it.hasNext())
			 {
				 
				 PluginDescriptor p = (PluginDescriptor) it.next();
				
				 
				 System.out.print( "\n"+ p.getId() + "\n" + constraint + "\n"); // this is set in the xml file
				 
				 if(p.getId().equals(constraint))
				 {
					 
				 IConstraint getconstraint = (IConstraint) pluginManager.getPlugin(p.getId());
				 return getconstraint;
				 }
			
			 }
		 } 
		 catch (Exception e) 
		 {
				e.printStackTrace();
		 }
		 return null;
		
	 }
	
	public static ISudoku getsudokuplugins(String type)
	{
		
		if(pluginManager == null)
		{
			loadPlugins();
		}
		 try
		 {
			 Iterator it = pluginManager.getRegistry().getPluginDescriptors().iterator();
			 System.out.print(it.toString());
			 while(it.hasNext())
			 {
				 
				 PluginDescriptor p = (PluginDescriptor) it.next();
				
				 
				 System.out.print( "\n"+ p.getId() + "\n" + type + "\n"); // this is set in the xml file
				 
				 if(p.getId().equals(type))
				 {
				
//				Object gettype = pluginManager.getPlugin(p.getId());
				
				 return (ISudoku) pluginManager.getPlugin(p.getId());
				 }
			
			 }
		 } 
		 catch (Exception e) 
		 {
				e.printStackTrace();
		 }
		 return null;
	}
	
	public static ISudoku getSudokuDrawingPlugins(String type)
	{
		
		if(pluginManager == null)
		{
			loadPlugins();
		}
		 try
		 {
			 Iterator it = pluginManager.getRegistry().getPluginDescriptors().iterator();
			 System.out.print(it.toString());
			 while(it.hasNext())
			 {
				 
				 PluginDescriptor p = (PluginDescriptor) it.next();
				
				 
				 System.out.print( "\n"+ p.getId() + "\n" + type + "\n"); // this is set in the xml file
				 
				 if(p.getId().equals(type))
				 {
				
//				Object gettype = pluginManager.getPlugin(p.getId());
				
				 return (ISudoku) pluginManager.getPlugin(p.getId());
				 }
			
			 }
		 } 
		 catch (Exception e) 
		 {
				e.printStackTrace();
		 }
		 return null;
	}


}
